/**
 * 询价.COM v1.0.0
 * http://xun-jia.com/
 *
 * Copyright (c) 2009 
 * 四川漾天空网络科技有限公司
 * 成都市天府软件园A3栋2楼
 *
 */
package com.xunjia.purchase.action;

import java.io.File;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.xunjia.action.XunJiaActionSupport;

/**
 * @author Mark
 * 
 * @Email Mark_yang@xun-jia.com
 * 
 * @Date 2009-4-9 下午01:45:29
 * 
 */
public class UploadFileAction extends XunJiaActionSupport {
	private static final long serialVersionUID = -6826419373669761586L;
	// 日志
	private static final Logger logger = Logger.getLogger(UploadFileAction.class);
	private static final String FILE_SAVE_DIR = "file.saveDir";
	private static final String FILE_CONFIG_NAME = "fileupload";
	private File uploadimg;
	private String uploadimgFileName;
	private String uploadimgContentType;

	public String execute() throws Exception {
		logger.info("UploadFileAction.............Start");
		// 获取文件配置
		ResourceBundle rb = ResourceBundle.getBundle(FILE_CONFIG_NAME);
		// 获取文件上传保存路径配置
		String saveDir = rb.getString(FILE_SAVE_DIR);
		// 获取保存路径
		String uploadDir = ServletActionContext.getServletContext().getRealPath(saveDir);
		// 创建路径
		File directory = new File(uploadDir);
		// 路径不存在时
		if (!directory.exists()) {
			// 创建不存在路径
			directory.mkdirs();
		}
		// 新建需要保存的新文件
		File dest = new File(uploadDir + "/" + uploadimgFileName);
		// 将上传文件保存至配置路径
		uploadimg.renameTo(dest);
		logger.info("UploadFileAction.............End");
		return Action.SUCCESS;
	}

	/**
	 * @return the uploadimg
	 */
	public File getUploadimg() {
		return uploadimg;
	}

	/**
	 * @param uploadimg
	 *            the uploadimg to set
	 */
	public void setUploadimg(File uploadimg) {
		this.uploadimg = uploadimg;
	}

	/**
	 * @return the uploadimgFileName
	 */
	public String getUploadimgFileName() {
		return uploadimgFileName;
	}

	/**
	 * @param uploadimgFileName
	 *            the uploadimgFileName to set
	 */
	public void setUploadimgFileName(String uploadimgFileName) {
		this.uploadimgFileName = uploadimgFileName;
	}

	/**
	 * @return the uploadimgContentType
	 */
	public String getUploadimgContentType() {
		return uploadimgContentType;
	}

	/**
	 * @param uploadimgContentType
	 *            the uploadimgContentType to set
	 */
	public void setUploadimgContentType(String uploadimgContentType) {
		this.uploadimgContentType = uploadimgContentType;
	}

}
